package net.fdukedom.epicurus.service.interaction;

import java.io.IOException;
import java.io.StringReader;

import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

/**
 * 
 * @author Alexander Kirakozov
 * 
 */
public class ResponseReader extends Response {
	/**
	 * XML response string
	 */
	private String xmlResponse;

	/**
	 * Constructor
	 */
	public ResponseReader(String response) {
		xmlResponse = response;
		createDocument();
	}

	@Override
	protected void createDocument() {
		try {
			SAXBuilder builder = new SAXBuilder();

			doc = builder.build(new StringReader(xmlResponse));
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Returns response status
	 * 
	 * @return response status
	 */
	public boolean getStatus() {
		String status = getElementValueByXPath(RESPONSE_PREFIX + STATUS);

		if (status.equals(STATUS_SUCCESS)) {
			return true;
		}

		return false;
	}

	/**
	 * Returns response message
	 * 
	 * @return response message
	 */
	public String getMessage() {
		return getElementValueByXPath(RESPONSE_PREFIX + MESSAGE);
	}

	/**
	 * Returns response result
	 * 
	 * @return response result
	 */
	public Element getResult() {
		return getElementByXPath(RESPONSE_PREFIX + RESULT);
	}
}
